/* CUC Online Judge - A cross-platform judge online system
 * Base on [Verwandlung Online Judge](https://github.com/hzxie/voj)
 * Copyright (C) 2021 Jinhe Zhang <307100890@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *                             /\/`\/\
 *                            _\ | | /_
 *                          -           -
 *                        /               \
 *                       | |-------------| |
 *                       | |^=-_#   #_-=^| |
 *                       \/ \O__/,_ \O__/ \/
 *                        \  \\\     \\\  /
 *                         _\     o     /_
 *                        |/ `--=___=--` \|
 *                       /              六 \
 *                      |  ____       __七_ |
 *                       \____/_\   /_\____/
 */
package org.verwandlung.voj.web.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.verwandlung.voj.web.mapper.StudentMapper;
import org.verwandlung.voj.web.mapper.UserMapper;
import org.verwandlung.voj.web.model.ClassC;
import org.verwandlung.voj.web.model.Student;
import org.verwandlung.voj.web.model.User;
import org.verwandlung.voj.web.util.MailSender;

/**
 * 用户类(User)的业务逻辑层.
 * 
 * @author Haozhe Xie
 */
@Service
@Transactional
public class StudentService {

	/**
	 * [此方法仅供教师使用]
	 * 获取班级学生数量.
	 * @param cid - 班级id
	 * @return 一个班级里面学生的数量
	 */
	public long getStudentCountof(Long cid) {
		return studentMapper.getStudentCountof(cid);
	}


	/**
	 * [此方法仅供教师使用]
	 * 获取班级学生.
	 * @param cid - 班级id
	 * @return 一个班级里面的所有学生
	 */
	public List<Student> getStudentsof(Long cid, long offset, int limit) {
		return studentMapper.getStudentsof(cid, offset, limit);
	}

	/**
	 * [此方法仅供本班级教师使用]
	 * 根据用户的唯一标识符删除用户.
	 * @param stuid - 用户的唯一标识符
	 */
	public User getUserofStudent(Student student) {
		return userMapper.getUserUsingStuId(student.getStudentCode());
	}

	/**
	 * [此方法仅供教师使用]
	 * 创建学生.
	 * @param username - 用户名
	 * @param password - 密码(未使用MD5加密)
	 * @return 一个包含账户创建结果的Map<String, Boolean>对象
	 */
	public Map<String, Boolean> createStudent(String name, String code, ClassC classc){
		Map<String, Boolean> result = new HashMap<String, Boolean>(3,1);
		Student student = new Student(name, code, classc);
		Boolean flag = studentMapper.createStudent(student) != 0?true:false;
		result.put("isSuccessful", flag);
		return result;
	}

	/**
	 * [此方法仅供本班级教师使用]
	 * 根据用户的唯一标识符删除用户.
	 * @param stuid - 用户的唯一标识符
	 */
	public void deleteStudent(long stuid) {
		studentMapper.deleteStudent(stuid);
	}

	/**
	 * 自动注入的studentMapper对象.
	 * 用于获取学生信息.
	 */
	@Autowired
	private StudentMapper studentMapper;

	/**
	 * 自动注入的userMapper对象.
	 * 用于获取用户信息.
	 */
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 自动注入的MailSender对象.
	 * 用于发送电子邮件至用户邮箱.
	 */
	@Autowired
	@Qualifier("vojMailSender")
	private MailSender mailSender;
}
